/******************************************************************************
Copyright:
2009 Jens Collin (jens.collin@dinomin.com)

This file is part of Envox Decompiler.

Envox Decompiler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Envox Decompiler is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KitchenOS.  If not, see <http://www.gnu.org/licenses/>.

******************************************************************************/
using System;

namespace EnvoxDecompiler
{
	/// <summary>
	/// Summary description for Block_RunHostedScript.
	/// </summary>
	public class Block_RunHostedScript : Block
	{

		private string _scriptType;
		private int _scriptFileOrSource;
		private string _data;

		public override TYPE GetBlockType()
		{
			return TYPE.RunHostedScript;
		}

		public override string GetXML(int x, int y)
		{
			Trace.Debug("Block_RunHostedScript::GetXML Started.");	
			// <RunScript name="NAME" rect="X 32 96 56"
            string xml = "\t\t\t<RunScript name=\"" + Utils.XmlString(_name) + "\" rect=\"" + x + " " + y + " 96 56\"";
			//  ...description="DESC"...
			xml += GetDescriptionXML();
			//  ...clearDTMF="_clearDTMFBuffer"...
			xml += GetClearDTMFXML();
			// ...minUserLevel="_userLevel"
			xml += GetMinUserLevelXML();
			// ...cpMessage=""...
			xml += GetControlPanelMessageXML();
			
			// ...scriptType="SCRIPT TYPE"...
			xml += " scriptType=\"" + _scriptType + "\"";
			// ...scriptFileOrSource="FILE OR SOURCE"...
			xml += " scriptFileOrSource=\"" + _scriptFileOrSource + "\"";
			// ...scriptFile="FILENAME"...
			xml += " scriptFile=\"" + Utils.XmlString(_data) + "\"";
			// ...scriptSource="SOURCE"...
			xml += " scriptSource=\"" + Utils.XmlString(_data) + "\"";

			// End the header xml-part.
			xml += ">\r\n";

			// <Inputs>
			xml += GetInputsXML(_blockId);
		
			// <Outputs>
			xml += GetOutputsXML();

			// <Logs>
			xml += GetLogsXML();
			
			xml += GetHandledErrorsXML();

			// </RunScript>
			xml +="\t\t\t</RunScript>\r\n";
			return xml;
		}


		/// <summary>
		/// Parse the string for outputs.
		/// String looks like this:
		/// "Output1:65535;output2:5;output3:7"
		/// </summary>
		/// <param name="data"></param>
		private void ParseOutputs(string data)
		{
			// TODO: What happens when a ':' or a ';' is within the key/value
			// stuff? Seems to be possible from studio pow.
			
			char[] del = new char[1];
			del[0] = ';';
			string[] split = data.Split(del);
			foreach (string pair in split)
			{
				Trace.Debug("Block_RunHostedScript ParseOutputs - Pair: " + pair);
				int pos = pair.IndexOf(":");
				if (pos != -1)
				{
					string key = pair.Substring(0, pos);
					string val = pair.Substring(pos+1);
					Trace.Debug("Block_RunHostedScript KV: " + key + " - " + val);
					AddLocalOutput(key, Convert.ToInt32(val));
				}
			}
		}


		public Block_RunHostedScript(byte[] rawData) : base(rawData)
		{
			// base has moved _currentPos to our start position.
			
			outputErrorIds.Add(new BlockOutput(OUTPUT.CantReadFile, 147));
			outputErrorIds.Add(new BlockOutput(OUTPUT.CantCreateEngine, 148));
			outputErrorIds.Add(new BlockOutput(OUTPUT.ParseError, 149));
			outputErrorIds.Add(new BlockOutput(OUTPUT.RunFailed, 150));
			outputErrorIds.Add(new BlockOutput(OUTPUT.Failed_2, 141));

			Trace.Debug("Block_RunHostedScript created.");

			// What type of source we have
			if (rawData[_currentPos + 8] != 0)
			{
				_scriptFileOrSource = 1;
			}
			else
			{
				_scriptFileOrSource = 0;
			}

			_currentPos += 16;
			
			// Script language
			_scriptType = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);
			
			// data
			_data = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			// outputs
			string outputString = Utils.DataToString(rawData, _currentPos);
			_currentPos = _currentPos + Utils.DataStringLength(rawData, _currentPos);

			ParseOutputs(outputString);

			Trace.Debug("Block_RunHostedScript: FileOrSource: " + _scriptFileOrSource +
				" Lang: " + _scriptType + " Data: " + _data);
		}
	}
}
